home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu745.dms / pu745.adf / GLOBE099.LHA / Ami-Globe / chemin.c < prev    next >
C/C++ Source or Header  |  1994-09-11  |  7KB  |  202 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*      fichier         : chemin.c                                      */
  4. /*      projet          : amiglobe                                      */
  5. /*      date création   : 29/08/94                                      */
  6. /*      commentaire     : calcul et dessin du plus court chemin entre   */
  7. /*                        le point origine et le point destination      */
  8. /*      révision        : $VER: chemin.c 1.011 (11 Sep 1994) */
  9. /*      copyright       : Olivier Collard, Thomas Landspurg             */
  10. /*                                                                      */
  11. /************************************************************************/
  12.  
  13.  
  14. /************************************************************************/
  15. /*      includes                                                        */
  16. /************************************************************************/
  17. #include <graphics/rastport.h>
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include <clib/graphics_protos.h>
  21. #include <stdio.h>
  22.  
  23. #include "amiglobe_types.h"
  24. #include "database_types.h"
  25. #include "map_function_protos.h"
  26. #include "chemin.h"
  27.  
  28. /************************************************************************/
  29. /*      defines                                                         */
  30. /************************************************************************/
  31. /* pas de parcours de l'angle alpha */
  32. #define PAS_ANGLE 100
  33.  
  34. /************************************************************************/
  35. /*      variables externes                                              */
  36. /************************************************************************/
  37. extern PREFERENCE Pref;
  38.  
  39. /************************************************************************/
  40. /*      variables privées                                               */
  41. /************************************************************************/
  42. enum pole { POLE_NORD, POLE_SUD}  Pole;
  43. enum sens { GAUCHE,DROITE } Sens;
  44. double gamma=0,alpha=0;
  45. double beta;
  46. double a,b,c;
  47. double tan_b_sur_2,tan_c_sur_2;
  48. double x,y;
  49. #define COLOR_CHEMIN 6
  50.  
  51. /************************************************************************/
  52. /*      implémentation                                                  */
  53. /************************************************************************/
  54.  
  55. /* ------------------------------- Update_Points -------------------------------
  56.  
  57.  Commentaire:met à jour les variables gamma,a,b et c en raison d'une
  58.              nouvelle position des points orig et dest
  59.              DOIT ETRE APPELE A CHAQUE MODIFICATION DES POINTS ORIG ET DEST
  60.              cf cerveau d'Olivier Collard pour comprendre
  61.  
  62. */
  63.  
  64. void
  65. Update_Points(void)
  66. {
  67.     /*  a,b et c sont les côtés d'un triangle sphérique. Le  point  (b,c) */
  68.     /*  est  soit  le  pôle  nord,  soit  le pôle sud (cf variable Pole). */
  69.     /*  point_orig est sur le point (a,c) et point_dest sur  (a,b)  alpha */
  70.     /*  est  l'angle au sommet (b,c) (pôle nord ou pôle sud) gamma, quant */
  71.     /*  à lui, est l'angle au sommet (a,b) (soit  point_dest)  a  est  le */
  72.     /*  plus  court  chemin entre point_orig et point_dest Le parcours de */
  73.     /*  l'angle alpha peut se faire soit vers la  gauche,  soit  vers  la */
  74.     /*  droite  (cf  variable Sens). Le parcours commence toujours depuis */
  75.     /*  le point destination.                                             */
  76.     b= 9000+Pref.point_dest.latitude;
  77.     b= b*PI/18000;
  78.     c= 9000+Pref.point_orig.latitude;
  79.     c *=PI/18000;
  80.     alpha=Pref.point_dest.longitude-Pref.point_orig.longitude;
  81.     if (alpha<0)
  82.     {
  83.         Sens=DROITE;/* point origine à droite du point destination */
  84.         alpha=-alpha;
  85.     }
  86.     else
  87.         Sens=GAUCHE;/*point origine à gauche du point destination */
  88.     if (alpha>18000)
  89.     {
  90.         if (Sens==GAUCHE)
  91.             Sens=DROITE;
  92.         else
  93.             Sens=GAUCHE;
  94.         alpha=36000-alpha;
  95.     }
  96.     alpha *=PI/18000;
  97.     /* calcul de a*/
  98.     a=acos(cos(b)*cos(c)+sin(b)*sin(c)*cos(alpha));
  99.     /* calcul de gamma */
  100.     gamma=acos((cos(c)-cos(a)*cos(b))/(sin(a)*sin(b)));
  101.     /* calcul de beta */
  102.     beta=acos(-cos(gamma)*cos(alpha)+sin(gamma)*sin(alpha)*cos(b));
  103.     /* détermination du pôle de référence Pole */
  104.     if (gamma+beta > PI)
  105.     {
  106.         Pole=POLE_SUD;
  107.         b=PI-b;
  108.         c=PI-c;
  109.         gamma=acos((cos(c)-cos(a)*cos(b))/(sin(a)*sin(b)));
  110.         beta=acos(-cos(gamma)*cos(alpha)+sin(gamma)*sin(alpha)*cos(b));
  111.     }
  112.     else
  113.         Pole=POLE_NORD;
  114.     /* pré-calcul des sin et des cos */
  115.     tan_c_sur_2=tan(c/2);
  116.     tan_b_sur_2=tan(b/2);
  117. }
  118.  
  119.  
  120. /* -------------------- Draw_Chemin(struct RastPort  rpG) ---------------------
  121.  
  122.  Commentaire:dessine le chemin le plus court entre Pref.point_orig et
  123.              Pref.point_dest
  124.  
  125. */
  126.  
  127. void
  128. Draw_Chemin(struct RastPort * rpG)
  129. {
  130.     if ( (Pref.point_orig.existe==TRUE) &&
  131.          (Pref.point_dest.existe==TRUE))
  132. /*  && (Pref.Flg_Aff_Chemin==TRUE)    */
  133.     {
  134.         /* on assume que Update_Points a été fait*/
  135.         double c1,a1;
  136.         /*,b1,a1; */
  137.         double angle=0,coupe=100;
  138.         int retard=0;
  139.         int x,y;
  140.         SetAPen(rpG,COLOR_CHEMIN);
  141.         MyMove(rpG,Pref.point_dest.longitude,Pref.point_dest.latitude);
  142.         if (Sens==GAUCHE)
  143.             if (Pref.point_dest.longitude-alpha*18000/PI < -18000)
  144.                 coupe=(Pref.point_dest.longitude+18000)*PI/18000;
  145.             else
  146.                 coupe=-100;
  147.         if (Sens==DROITE
  148.             && Pref.point_dest.longitude+alpha*18000/PI > 18000)
  149.             coupe=(18000-Pref.point_dest.longitude)*PI/18000;
  150.         while (abs(angle)<abs(alpha))
  151.         {
  152.             a1=asin(sin(angle)*sin(b)*sin(beta));
  153.             if (gamma+angle>PI)
  154.             {
  155.                 c1= atan(tan_c_sur_2*cos((abs(alpha-angle)-beta)/2)
  156.                                  /cos((abs(alpha-angle)+beta)/2))
  157.                   - atan(tan_c_sur_2*sin((abs(alpha-angle)-beta)/2)
  158.                                  /sin((abs(alpha-angle)+beta)/2));
  159.             }
  160.             else
  161.             {
  162.                 c1= atan(tan_b_sur_2*cos((gamma-angle)/2)/cos((gamma+angle)/2))
  163.                   + atan(tan_b_sur_2*sin((gamma-angle)/2)/sin((gamma+angle)/2));
  164.             }
  165.             if (Sens==GAUCHE)
  166.                 x=Pref.point_dest.longitude-angle*18000/PI-retard;
  167.             else
  168.                 x=Pref.point_dest.longitude+angle*18000/PI-retard;
  169.             switch (Pole)
  170.             {
  171.             case POLE_NORD:
  172.                 y=-9000+c1*18000/PI;
  173.                 break;
  174.             case POLE_SUD:
  175.                 y=9000-c1*18000/PI;
  176.                 break;
  177.             }
  178.             MyDraw(rpG,x,y,5);
  179.             if (angle==coupe)
  180.             {
  181.                 if (Sens == GAUCHE )
  182.                 {
  183.                     coupe=-100;
  184.                     retard=-36000;
  185.                     MyMove(rpG,18000,y);
  186.                 }
  187.                 else
  188.                 {
  189.                     coupe =100;
  190.                     retard=36000;
  191.                     MyMove(rpG,-18000,y);
  192.                 }
  193.             }
  194.             angle+=PI*Pref.prof/PAS_ANGLE;
  195.             if (    (angle>coupe && Sens==DROITE)
  196.                  || (angle<coupe && Sens==GAUCHE)     )
  197.                 angle=coupe;
  198.         }
  199.         MyDraw(rpG,Pref.point_orig.longitude,Pref.point_orig.latitude,5);
  200.     }
  201. }
  202.